<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="菜品规格列表"
      :columns="columns"
      :request-api="getFoodsPecInfoList"
      :init-param="initParam"
      :data-callback="dataCallback"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'addtask'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新建规格 </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="Paperclip" @click="openGoodsDialog(scope.row)"> 关联菜品 </el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteAccount(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <SpecificationDialog ref="specDialogRef" />
    <SelectTreeData ref="goodsDialogRef" />
  </div>
</template>

<script setup lang="tsx" name="SpecificationManage">
import { ref, reactive } from "vue";
import { Goods } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import SpecificationDialog from "@/views/goods/components/specificationDialog.vue";
import SelectTreeData from "@/components/SelectTreeData/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, Paperclip } from "@element-plus/icons-vue";
import { deleteUser, editUser, addUser } from "@/api/modules/user";
import { getFoodsPecInfoList } from "@/api/modules/goods";

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable(之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({ type: 1 });

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，那么你可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};

// 表格配置项
const columns: ColumnProps<Goods.ResGoodsSpecificationsList>[] = [
  { type: "index", label: "序号", width: 60 },
  { prop: "name", label: "规格名称", align: "left", search: { el: "input" } },
  { prop: "refGoodsCount", label: "关联数量", align: "left", width: 120 },
  { prop: "operation", label: "操作", align: "left", fixed: "right", width: 270 }
];

// 删除用户信息
const deleteAccount = async (params: Goods.ResGoodsSpecificationsList) => {
  await useHandleData(deleteUser, { id: [params.id] }, `删除【${params.name}】属性`);
  proTable.value?.getTableList();
};

// 选择关联菜品
const goodsDialogRef = ref<InstanceType<typeof SelectTreeData> | null>(null);
const openGoodsDialog = (row: Partial<Goods.ResGoodsSpecificationsList> = {}) => {
  const params = {
    isView: true,
    row: { ...row },
    api: addUser,
    getTableList: proTable.value?.getTableList
  };
  goodsDialogRef.value!.acceptParams(params);
};

// 打开 drawer(新增、查看、编辑)
const specDialogRef = ref<InstanceType<typeof SpecificationDialog> | null>(null);
const openDrawer = (title: string, row: Partial<Goods.ResGoodsSpecificationsList> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: title === "新增" ? addUser : title === "编辑" ? editUser : undefined,
    getTableList: proTable.value?.getTableList
  };
  specDialogRef.value?.acceptParams(params);
};
</script>
